package com.leador.map.api.location;

import android.content.Context;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;

import java.util.Vector;

import com.leador.map.api.location.core.CoreUtil;

/**
 *
 */
public class MapLocationManager {
    private static MapLocationManager _instance = null;
    //
    public final static int LOCATION_MESSAGE_SUCCESS = 100;
    public final static int LOCATION_MESSAGE_FAIL = 101;
    //
    private Context context;

    private static Vector<RequestLocationEntity> requestlocations = null;
    private MapLocationHandler mapLocationHandler = null;

    /**
     * Gps定位管理器
     */
    private static GpsLocationManager gpsManager = null;
    /**
     * 网络定位管理器
     */
    private static NetworkLocationManager networkLocManager = null;
    /**
     * 当前位置
     */
    private MapLocation currLocation;
    private Thread locThread;
    public static boolean mGpsFixed = false;
    public static long mLastGpsFixedTime;
    //
    public static boolean locEnable = true;
    public static boolean mGpsEnable = true;

    /**
     * 构造函数
     * @param context
     * @param androidLocManager
     */
    private MapLocationManager(Context context,LocationManager androidLocManager) {
        this.context = context;
        requestlocations = new Vector();
        //
        mapLocationHandler = new MapLocationHandler();
        //
        gpsManager = GpsLocationManager.getInstance(context, androidLocManager,
                mapLocationHandler);
        //
        networkLocManager = NetworkLocationManager.getInstance(context, mapLocationHandler);
    }

    /**
     * 获取单实例
     * @param context
     * @param androidLocManager
     * @return
     */
    public static MapLocationManager getInstance(Context context,
                                                 LocationManager androidLocManager) {
        synchronized (MapLocationManager.class) {
            if (_instance == null) {
                _instance = new MapLocationManager(context, androidLocManager);
            }
        }
        return _instance;
    }

    /**
     * 获得上次的已知定位位置
     * @return
     */
    public MapLocation getLastKnownLocation() {
        if (currLocation != null) {
            return currLocation;
        }
        return CoreUtil.getPrefsLocation(context);
    }

    /**
     * @param minTime 最小定位间隔
     * @param minDistance 最小定位间隔距离
     * @param listener
     * @param provider
     */
    public void requestLocationUpdates(long minTime, float minDistance,
                                       MapLocationListener listener, String provider) {
        if(listener==null||provider==null||provider.length()==0){
            return ;
        }
        RequestLocationEntity requestEntity = new RequestLocationEntity(
                minTime, minDistance, listener, provider);
        requestlocations.add(requestEntity);
        //
        if (provider.equalsIgnoreCase(LocationManagerProxy.GPS_PROVIDER)) {
            //仅GPS
            gpsManager.requestLocationUpdates(minTime, minDistance, listener,
                    provider);
        } else if (provider.equalsIgnoreCase(LocationProviderProxy.LocNetwork)) {
            //GPS与网络混合
            if (mGpsEnable) {
                gpsManager.requestLocationUpdates(minTime, minDistance,
                        listener, provider);
            }
            //网络定位，在线程中执行，有结果进行回调
            networkLocManager.setRequestTime(minTime);
            locEnable = true;
            if (locThread == null) {
                locThread = new Thread(networkLocManager);
                locThread.start();
            }
        }
    }

    /**
     *
     * @param listener
     */
    public void removeUpdates(MapLocationListener listener) {
        if(listener==null){
            return ;
        }
        MapLocationListener maplocListener = listener;
        int size = requestlocations.size();
        RequestLocationEntity requestEntity = null;
        for (int i = 0; i < size; i++) {
            requestEntity = requestlocations.get(i);
            if (maplocListener.equals(requestEntity.listener)) {
                requestlocations.remove(requestEntity);
                size--;
            }
        }
        if (gpsManager != null && requestlocations.size() == 0) {
            gpsManager.removeGPSUpdates();
            mGpsFixed = false;
            locEnable = false;
        }
    }

    /**
     * 设置Gps状态
     * @param enable true-使用,false-不使用
     */
    public void setGpsEnable(boolean enable) {
        mGpsEnable = enable;
        //是否使用Gps
        if (networkLocManager != null) {
            networkLocManager.setUseGps(enable);
        }
    }

    public void destroy() {
        if (networkLocManager != null) {
            networkLocManager.destroy();
            networkLocManager = null;
        }
        if (gpsManager != null) {
            gpsManager.removeGPSUpdates();
            gpsManager = null;
        }
        requestlocations.clear();
        mGpsFixed = false;
        locThread = null;
        _instance = null;
    }

    /**
     *
     */
    class MapLocationHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {

            if (msg.what == LOCATION_MESSAGE_SUCCESS) {
                for (RequestLocationEntity requestEntity : requestlocations) {
                    requestEntity.listener.onLocationChanged((MapLocation) msg.obj);
                }
                currLocation = (MapLocation) msg.obj;
                if (currLocation != null) {
                    CoreUtil.setPrefsLocation(context, currLocation);
                }
            }else if(msg.what == LOCATION_MESSAGE_FAIL){
                for (RequestLocationEntity requestEntity : requestlocations) {
                    requestEntity.listener.onLocationChanged((MapLocation) msg.obj);
                }
            }
        }
    }
}
